<%@ page import="com.surelution.projects.crm.Market" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="layout" content="main" />
        <g:javascript library="jquery" plugin="jquery"/>
		<resource:dateChooser />
		<script type="text/javascript" src="${resource(dir:'js/datechooser',file:'datechooser.js')}"></script>
        <g:set var="entityName" value="${message(code: 'market.label', default: 'Market')}" />
        <title><g:message code="default.create.label" args="[entityName]" /></title>
    </head>
    <body>
        <div class="nav">
            <span class="menuButton"><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></span>
            <span class="menuButton"><g:link class="list" action="list"><g:message code="default.list.label" args="[entityName]" /></g:link></span>
        </div>
        <div class="body">
            <h1><g:message code="default.create.label" args="[entityName]" /></h1>
            <g:if test="${flash.message}">
            <div class="message">${flash.message}</div>
            </g:if>
            <g:hasErrors bean="${marketInstance}">
            <div class="errors">
                <g:renderErrors bean="${marketInstance}" as="list" />
            </div>
            </g:hasErrors>
            <g:form action="save" >
                <div class="dialog" >
                    <table >
                        <tbody>
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="name">*<g:message code="market.name.label" default="Name" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'name', 'errors')}">
                                    <g:textField name="name" value="${marketInstance?.name}" />
                                </td>
                            </tr>

                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="type"><g:message code="market.type.label" default="Type" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'type', 'errors')}">
                                    <g:select name="type.id" from="${com.surelution.projects.crm.MarketType.list()}" optionKey="id" optionValue="title" value="${marketInstance?.type?.id}"  />
                                </td>
                            </tr> 
                            
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="marketType"><g:message code="market.marketType.label" default="marketType" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'marketType', 'errors')}">
                                    <g:select name="marketType.id" from="${com.surelution.projects.crm.MarketSpecies.list().findAll{it.id != 2}}" optionKey="id" optionValue="type" value="${marketInstance?.marketType?.id}"  />
                                </td>
                            </tr> 
                                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="address">*<g:message code="market.address.label" default="Address" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'address', 'errors')}">
                                    <g:textField name="address" value="${marketInstance?.address}" />
                                </td>
                            </tr>
                            
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="name">*<g:message code="market.manager.label" default="manager" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'manager', 'errors')}">
                                    <g:textField name="manager" value="${marketInstance?.manager}" />
                                </td>
                            </tr>
                            
                           <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="name">*<g:message code="market.managerTel.label" default="managerTel" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'managerTel', 'errors')}">
                                    <g:textField name="managerTel" value="${marketInstance?.managerTel}" />
                                </td>
                            </tr>        
                            
                            <tr class="prop">
                                <td valign="top" class="name">
                                  <label for="carePoint"><g:message code="market.carePoint.label" default="carePoint" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'carePoint', 'errors')}">
                                    <g:textField name="carePoint" value="${fieldValue(bean: marketInstance, field: 'carePoint')}" />
                                </td>
                            </tr>
                            
                            <tr class="prop">
                                <td valign="top" class="name">
                                  <label for="provideSite"><g:message code="market.provideSite.label" default="provideSite" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'provideSite', 'errors')}">
                                    <g:textField name="provideSite" value="${fieldValue(bean: marketInstance, field: 'provideSite')}" />
                                </td>
                            </tr>
                            
                            <tr class="prop">
                                <td valign="top" class="name">
                                  <label for="betweenDistance"><g:message code="market.betweenDistance.label" default="betweenDistance" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'betweenDistance', 'errors')}">
                                    <g:textField name="betweenDistance" value="${fieldValue(bean: marketInstance, field: 'betweenDistance')}" />
                                </td>
                            </tr>
                            
                            <tr class="prop">
                                <td valign="top" class="name">
                                  <label for="requirement"><g:message code="market.requirement.label" default="requirement" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'requirement', 'errors')}">
                                    <g:textField name="requirement" value="${fieldValue(bean: marketInstance, field: 'requirement')}" />
                                </td>
                            </tr>
                            
                             <tr class="prop">
                                <td valign="top" class="name">
                                  <label for="gasPredictSale"><g:message code="market.gasPredictSale.label" default="gasPredictSale" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'gasPredictSale', 'errors')}">
                                    <g:textField name="gasPredictSale" value="${fieldValue(bean: marketInstance, field: 'gasPredictSale')}" />
                                </td>
                            </tr>
                            
                             <tr class="prop">
                                <td valign="top" class="name">
                                  <label for="fuelPredictSale"><g:message code="market.fuelPredictSale.label" default="fuelPredictSale" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'fuelPredictSale', 'errors')}">
                                    <g:textField name="fuelPredictSale" value="${fieldValue(bean: marketInstance, field: 'fuelPredictSale')}" />
                                </td>
                            </tr>
                             
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="name"><g:message code="market.startTime.label" default="startTime" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'startTime', 'errors')}">
                                    <richui:dateChooser  locale="zh" name="startTime" format="yyyy-MM-dd" value="${new Date()}"/>
                                </td>
                            </tr>   
                            
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="name"><g:message code="market.firstSaleTime.label" default="firstSaleTime" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'firstSaleTime', 'errors')}">
                                    <richui:dateChooser name="firstSaleTime" locale="zh" format="yyyy-MM-dd" value="${new Date()}"/>
                                </td>
                            </tr>   

                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="latitude"><g:message code="market.latitude.label" default="Latitude" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'latitude', 'errors')}">
                                    <g:textField name="latitude" id="lat" value="${marketInstance?.latitude}" />
                                </td>
                            </tr>
                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="longitude"><g:message code="market.longitude.label" default="Longitude" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'longitude', 'errors')}">
                                    <g:textField name="longitude" id="lng" value="${marketInstance?.longitude}" />
                                </td>
                            </tr>
                        
                            <tr class="prop" >
                                <td valign="top" class="name">
                                    <label for="vehicleCount"><g:message code="market.discountNum.label" default="discountNum" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: marketInstance, field: 'vehicleCount', 'errors')}">
                                    <g:textField name="discountNum" value="${fieldValue(bean: marketInstance, field: 'discountNum')}" />
                                </td>
                            </tr>
                            
                        </tbody>
                    </table>
                </div>
                <div class="buttons">
                    <span class="button"><g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" /></span>
                </div>
            </g:form>
            <div class="left_body" style="position:relative;top:-650px;left:350px;">
				<label style="color:blue;font-size:16px;padding:5px 0px;">拖动红色小气球，可在地图上为客户定位</label>
			<div id="SmallMap"></div>
		</div>
        </div>
		<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>
		<script type="text/javascript">
			var map;
			function initMap() {

				var lat=30.748013;
				var lng=120.741578;
				var type="BMAP_HYBRID_MAP";
				var zoom=13;
				var url=location.search;
				if(url.indexOf("?")!=-1)   
				{   
				  var str = url.substr(1);
				  strs = str.split("&");   
				  for(i=0;i<strs.length;i++)   
				  {   
				    if([strs[i].split("=")[0]]=='lat') lat=unescape(strs[i].split("=")[1]);
				    if([strs[i].split("=")[0]]=='lng') lng=unescape(strs[i].split("=")[1]);
				    if([strs[i].split("=")[0]]=='zoom') zoom=unescape(strs[i].split("=")[1]);
				    if([strs[i].split("=")[0]]=='mapType') type=unescape(strs[i].split("=")[1]);
				  }   
				}
				map = new BMap.Map("SmallMap");
				if (type == "BMAP_NORMAL_MAP"){
					map.setMapType(BMAP_NORMAL_MAP);
					}
				if (type == "BMAP_SATELLITE_MAP"){
					map.setMapType(BMAP_SATELLITE_MAP);
					}
				if (type == "BMAP_HYBRID_MAP"){
					map.setMapType(BMAP_HYBRID_MAP);
					}
				var center = new BMap.Point(lng, lat);
				map.centerAndZoom(center, zoom);
				map.addControl(new BMap.NavigationControl());
				map.addControl(new BMap.MapTypeControl({mapTypes:[BMAP_NORMAL_MAP, BMAP_SATELLITE_MAP, BMAP_HYBRID_MAP]}));
				addMarker(lng,lat);
				
			}			

			function addMarker(x, y){
				var myIcon = new BMap.Icon(
						"http://api.map.baidu.com/img/markers.png" , 
						new BMap.Size(23, 25), 
						{offset: new BMap.Size(32, 32),size:new BMap.Size(23, 25)}
				);
				var point = new BMap.Point(x, y);
				var marker = new BMap.Marker(point, {icon: myIcon}); 
				marker.enableDragging(true);
				marker.addEventListener("dragend", function(e){
					var lng = e.point.lng;
					var lat = e.point.lat;
					document.getElementById("lat").value=lat;  
					document.getElementById("lng").value=lng;  
				});
				map.addOverlay(marker);
			}
			
			$(document).ready(function() {
				initMap();
			 });
			</script>	

    </body>
</html>
